{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.convolutional import Conv2D\n",
    "from keras.layers.pooling import MaxPooling2D, AveragePooling2D\n",
    "from keras.layers.normalization import BatchNormalization\n",
    "from keras import backend as K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=8):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'expected': {'shape': [6, 6, 4], 'data': [0.09770863, 0.69939429, 1.31375301, 3.16071463, 0.36043367, 1.33017242, 0.89586127, 3.77320218, 0.36043367, 1.33959401, 0.06480566, 3.12202501, 0.2979188, 0.42151114, 0.25360936, 2.09999943, 0.2979188, 0.41208959, 0.50230438, 2.01106024, 0.0, 0.0, 0.62700135, 2.03990436, 0.0, 2.03871799, 1.31483877, 4.37769318, 0.36043367, 2.23974395, 1.10517418, 4.1841898, 0.36043367, 1.69027638, 0.47355592, 2.06088901, 0.54417002, 0.37750795, 0.47355592, 0.85943532, 0.54417002, 0.14150141, 0.85223472, 2.01370406, 0.0, 0.0, 1.70446944, 3.30465341, 0.0, 2.32790756, 0.54632819, 1.79699683, 0.09412091, 1.36912918, 1.32046115, 1.29625559, 0.09412091, 0.81966186, 2.3462801, 1.27921855, 0.27149075, 0.75216007, 1.48125052, 1.84846115, 0.27149075, 0.51615357, 1.84966826, 1.8253777, 0.0, 0.0, 3.69933653, 2.05699205, 0.0, 0.98858374, 0.0, 1.58660531, 0.09412091, 0.45955783, 1.21049559, 2.43468308, 0.09412091, 0.45955783, 2.2829957, 3.11471605, 0.02523952, 1.43037307, 1.66019785, 3.08398438, 0.02523952, 2.12547445, 2.25083542, 2.4713769, 0.0, 1.39020252, 3.32627583, 2.54162073, 0.7954644, 0.37762421, 0.24785733, 2.89983678, 0.0, 0.37762421, 1.05194378, 4.48739719, 0.0, 0.08148286, 0.84053111, 4.38473654, 0.0, 1.13720381, 1.12069368, 4.17663002, 0.0, 2.01437855, 1.47289729, 3.57055497, 0.0, 1.91731548, 0.70440722, 4.11429119, 1.59092879, 0.75524843, 0.49571466, 3.35791111, 0.0, 0.75524843, 1.41295576, 4.26912975, 0.0, 0.16296571, 0.99013048, 4.68362713, 0.0, 0.16296571, 1.06599212, 5.44113255, 0.0, 0.52711296, 1.06599212, 3.89428544, 0.0, 1.05422592, 0.0, 3.98528647]}, 'input': {'shape': [8, 8, 2], 'data': [0.23818833, 0.38642283, 0.73048693, -0.47526663, 0.2436712, 0.51476225, -0.87651417, -0.93096866, 0.39945658, -0.62878551, 0.47696322, 0.82052012, -0.69410474, -0.51176329, -0.03493528, -0.67724651, -0.1438784, -0.41871218, 0.5317537, -0.45119741, -0.47477776, -0.02791437, -0.73532644, 0.85052386, -0.83494754, -0.484916, -0.68271897, -0.31324565, 0.19098531, 0.09672627, 0.60204791, 0.75362631, 0.03751795, 0.17711952, -0.18818308, -0.42192077, 0.30960568, 0.30260288, 0.69904695, 0.40839006, -0.72837934, -0.34130458, 0.28476025, -0.61902603, -0.36483878, 0.16180785, 0.16528898, 0.79756699, -0.55712896, 0.92866647, 0.98234023, 0.20342651, 0.02120589, -0.52837501, 0.44826198, -0.13450526, 0.0007302, 0.48821493, 0.89909807, -0.13044485, 0.59356896, -0.04250332, 0.49117919, 0.84268367, 0.10787048, 0.37060042, -0.95726539, 0.46534069, -0.44968491, -0.88017385, 0.15150885, -0.54432102, -0.8264465, -0.70648452, -0.07361012, -0.89055831, 0.04869285, 0.78770545, 0.32467293, -0.06777691, 0.4797106, -0.25898307, -0.06452129, -0.38920257, -0.60029947, 0.57611968, 0.69333207, 0.747125, 0.11704734, 0.91604161, -0.4584781, 0.34976772, -0.98476146, -0.73474272, -0.48142895, -0.74601445, -0.85123631, -0.23762887, 0.26503319, 0.84841393, 0.14321404, -0.69321338, -0.78219634, 0.79360567, -0.55594312, -0.57403329, -0.62931872, 0.75071639, -0.02673536, 0.28903882, -0.73712451, 0.21345747, -0.53844965, -0.89673972, -0.71726017, -0.14742523, -0.87849337, 0.47208457, -0.00030255, -0.75982615, 0.1673542, -0.00072796, 0.96937592, 0.90875246, 0.23688769, -0.9176563, 0.00490324, 0.05307557]}, 'weights': [{'shape': [3, 3, 2, 4], 'data': [0.23818833, 0.38642283, 0.73048693, -0.47526663, 0.2436712, 0.51476225, -0.87651417, -0.93096866, 0.39945658, -0.62878551, 0.47696322, 0.82052012, -0.69410474, -0.51176329, -0.03493528, -0.67724651, -0.1438784, -0.41871218, 0.5317537, -0.45119741, -0.47477776, -0.02791437, -0.73532644, 0.85052386, -0.83494754, -0.484916, -0.68271897, -0.31324565, 0.19098531, 0.09672627, 0.60204791, 0.75362631, 0.03751795, 0.17711952, -0.18818308, -0.42192077, 0.30960568, 0.30260288, 0.69904695, 0.40839006, -0.72837934, -0.34130458, 0.28476025, -0.61902603, -0.36483878, 0.16180785, 0.16528898, 0.79756699, -0.55712896, 0.92866647, 0.98234023, 0.20342651, 0.02120589, -0.52837501, 0.44826198, -0.13450526, 0.0007302, 0.48821493, 0.89909807, -0.13044485, 0.59356896, -0.04250332, 0.49117919, 0.84268367, 0.10787048, 0.37060042, -0.95726539, 0.46534069, -0.44968491, -0.88017385, 0.15150885, -0.54432102]}, {'shape': [4], 'data': [0.23109399, -0.67901645, -0.21010232, -0.26585777]}, {'shape': [3, 3, 4, 4], 'data': [-0.97904373, 0.12432939, -0.47181983, 0.42632265, -0.24040861, -0.32441341, -0.92514709, -0.75841704, 0.92005259, 0.57887489, 0.67712385, 0.6107204, 0.32786335, -0.24750625, -0.0834088, 0.45101158, 0.11567933, 0.7733556, -0.73516134, 0.92569722, 0.69394877, 0.72240363, -0.60719205, 0.73622876, -0.69013032, 0.86215256, -0.03776081, -0.41550456, -0.69154572, -0.86183061, 0.41992827, -0.2025777, -0.35606894, -0.65210622, -0.90330948, 0.16793129, 0.62235708, -0.12913986, 0.47830428, 0.25139334, 0.73489784, -0.34638153, 0.53212037, 0.48741541, 0.19016872, 0.29668604, 0.16919531, 0.98282918, -0.45381671, 0.52101707, 0.47559285, 0.66132051, 0.95922486, 0.26475694, -0.97006494, -0.87271599, -0.28464485, 0.03126879, -0.26446835, 0.65891668, 0.0028847, 0.28319858, 0.25018971, 0.54665979, 0.18506646, 0.49659109, -0.31484708, -0.68768762, -0.77700913, 0.51171875, 0.73744201, 0.89339484, -0.78788259, -0.11855415, -0.44430538, 0.87980648, -0.59180545, -0.37405247, 0.26130953, 0.43750108, -0.95532416, -0.00477862, -0.72382127, -0.66304314, 0.26543647, 0.03901949, -0.14944003, 0.67195524, -0.29661672, -0.78911287, 0.9812027, 0.09859503, 0.31014364, -0.73706155, -0.53823012, 0.37211228, -0.30860357, -0.7479455, 0.2443897, 0.8407575, -0.8397628, 0.36113691, 0.56581712, -0.45278977, 0.32038649, 0.21525444, -0.17700933, 0.72031075, -0.46300401, 0.02055388, 0.74494174, 0.05274687, -0.40431444, 0.86148064, 0.22523994, 0.45554139, -0.70763437, 0.82049153, -0.74294366, 0.2292485, -0.34269546, 0.1538307, 0.14037827, -0.87557855, 0.82423413, -0.31348373, 0.09257801, 0.08841733, 0.59324896, 0.56300498, 0.53972192, -0.06845718, 0.86770803, -0.73565169, 0.3640887, -0.12001947, -0.91635359, 0.74197951, -0.28725129, -0.14546392, 0.56735682, -0.82124446, 0.08111002, -0.05727465]}, {'shape': [4], 'data': [-0.03959753, -0.58719662, 0.64229975, -0.91488481]}]}\n"
     ]
    }
   ],
   "source": [
    "data_in_shape = (8, 8, 2)\n",
    "\n",
    "conv_0 = Conv2D(4, 3, 3, activation='relu', border_mode='same', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "conv_1 = Conv2D(4, 3, 3, activation='relu', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "pool_0 = AveragePooling2D(pool_size=(2, 2), strides=(1, 1), border_mode='same', dim_ordering='tf')\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = conv_0(input_layer)\n",
    "x = conv_1(x)\n",
    "output_layer = pool_0(x)\n",
    "model = Model(input=input_layer, output=output_layer)\n",
    "\n",
    "np.random.seed(13000)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(13000 + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "\n",
    "print({\n",
    "    'input': {'data': format_decimal(data_in.ravel().tolist()), 'shape': list(data_in_shape)},\n",
    "    'weights': [{'data': format_decimal(weights[i].ravel().tolist()), 'shape': list(weights[i].shape)} for i in range(len(weights))],\n",
    "    'expected': {'data': format_decimal(result[0].ravel().tolist()), 'shape': list(result[0].shape)}\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
